﻿Imports System.Data.OleDb

Public Class frmGestionLocations

    Dim locations As New ArrayList

    Dim bdLocations As New cls_GestionBD("taco.mdb")

    Private Sub btnQuitter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQuitter.Click
        Me.Close()
    End Sub

    Private Sub btnAjouter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAjouter.Click
        idClient = Nothing
        frmDetailsLocation.btnTerminer.Text = "Terminer"
        frmDetailsLocation.ShowDialog(New clsLocation())
    End Sub

    Private Sub btnModifier_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnModifier.Click
        For Each location As clsLocation In locations
            If location.noLocation = lstResultats.SelectedItems(0).Text Then
                frmDetailsLocation.btnTerminer.Text = "Enregistrer"
                frmDetailsLocation.ShowDialog(location)
                Exit For
            End If
        Next
    End Sub

    Private Sub btnSupprimer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSupprimer.Click
        For Each location As clsLocation In locations
            If location.noLocation = lstResultats.SelectedItems(0).Text Then
                location.actif = False
                location.enregistrer()
                remplirListView()
                Exit For
            End If
        Next
    End Sub

    Private Sub btnRechercheDetaillee_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRechercheDetaillee.Click
        'affiche ou fait disparaitre le menu de recherche détaillée
        If conRechercheDetaille.Visible = True Then
            conRechercheDetaille.Visible = False
            lstResultats.Height += conRechercheDetaille.Height
            lstResultats.Top -= conRechercheDetaille.Height
            btnRechercheDetaillee.Text = "/\"
        Else
            conRechercheDetaille.Visible = True
            lstResultats.Height -= conRechercheDetaille.Height
            lstResultats.Top += conRechercheDetaille.Height
            btnRechercheDetaillee.Text = "\/"
        End If
    End Sub

    Private Sub frmGestionLocations_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'ferme l'option de recherche détaillé
        btnRechercheDetaillee.PerformClick()
        cboAfficherLocations.Text = "Tous"
        remplirListView()
    End Sub

    Private Sub btnFacturer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFacturer.Click
        'noVehicule = lstResultats.SelectedItems(0).SubItems(2).Text
        'noLocation = lstResultats.SelectedItems(0).Text

        For Each location As clsLocation In locations
            If location.noLocation = lstResultats.SelectedItems(0).Text Then
                frmCloreLocation.ShowDialog(location)
                Exit For
            End If
        Next

    End Sub

    Sub remplirListView()

        Dim bdLocations As New cls_GestionBD("taco.mdb")
        bdLocations.daBd.SelectCommand = New OleDbCommand( _
            "SELECT * " & _
            "FROM tblLocations " & _
            "WHERE actif = true ")

        Select Case cboAfficherLocations.Text
            Case "Tous"

            Case "Commencent aujourd'hui"
                bdLocations.daBd.SelectCommand.CommandText &= "AND dateDebut = @dateDuJour "
                bdLocations.daBd.SelectCommand.Parameters.Add("@dateDuJour", OleDbType.Date).Value = FormatDateTime(Now, DateFormat.ShortDate)

            Case "Finissent aujourd'hui"
                bdLocations.daBd.SelectCommand.CommandText &= "AND dateFinLocation = @dateDuJour "
                bdLocations.daBd.SelectCommand.Parameters.Add("@dateFinLocation", OleDbType.Date).Value = FormatDateTime(Now, DateFormat.ShortDate)

            Case Else
                MsgBox("Erreur remplirListView: " & cboAfficherLocations.Text)
        End Select


        bdLocations.daBd.SelectCommand.CommandText &= "ORDER BY dateFinLocation ASC"

        'remplit le dataSet avec la requête SQL du dataAdapter
        bdLocations.remplirDataSet("tblLocations")

        'vide la listView
        lstResultats.Items.Clear()
        locations.Clear()

        For Each row As DataRow In bdLocations.dsBd.Tables("tblLocations").Rows
            locations.Add(New clsLocation(row))
        Next

        For Each location As clsLocation In locations

            Dim LI As New ListViewItem(location.noLocation)

            LI.SubItems.Add(location.dateFin)
            LI.SubItems.Add(location.dateDebut)

            Dim bd As New cls_GestionBD("taco.mdb")
            bd.remplirDataSet("select * from tblClients where noClient = " & location.noClient, "tbl")
            LI.SubItems.Add(bd.dsBd.Tables("tbl").Rows(0).Item("prenom") & " " & bd.dsBd.Tables("tbl").Rows(0).Item("nom"))

            'LI.SubItems.Add(location.noClient)
            LI.SubItems.Add(location.vehicule.marque & " " & location.vehicule.modele & " " & location.vehicule.annee)

            lstResultats.Items.Add(LI)

        Next

        ''vide la listView
        'lstResultats.Clear()

        ''créé une colonne dans la listView pour chaque élément de la requête SQL
        'For ctr As Integer = 0 To bdLocations.dsBd.Tables("tblLocations").Columns.Count - 1
        '    lstResultats.Columns.Add(bdLocations.dsBd.Tables("tblLocations").Columns(ctr).ColumnName, 130)
        '    If lstResultats.Columns(ctr).Text = "noVehicule" Or lstResultats.Columns(ctr).Text = "noLocation" Then
        '        'lstResultats.Columns(ctr).Width = 0
        '    End If
        'Next

        ''créé un élément dans le listView pour chaque enregistrement retourné par la requête SQL
        'For Each location As DataRow In bdLocations.dsBd.Tables("tblLocations").Rows

        '    'on créé un nouvel item
        '    Dim LI As New ListViewItem
        '    LI.Text = location.Item(0)

        '    'on lui ajouter toutes les informations de l'enregistrement
        '    For ctr As Integer = 1 To bdLocations.dsBd.Tables("tblLocations").Columns.Count - 1
        '        LI.SubItems.Add(nvl(location.Item(ctr), ""))
        '    Next

        '    lstResultats.Items.Add(LI)

        'Next

    End Sub

    Private Sub btnChercher_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChercher.Click
        remplirListView()
    End Sub
End Class